<?php
/**
 * Author: 周斌斌 <553193368@qq.com>
 * Date: 17/3/21 PM3:31
 * 摘要:
 */

namespace app\ice\controller;


use org\BaiduMap;
use think\Db;
use think\Exception;

class Common extends Init
{
    protected $areaName = [
        1 => '华东地区',
        2 => '华南地区',
        3 => '华中地区',
        4 => '华北地区',
        5 => '西北地区',
        6 => '西南地区',
        7 => '东北地区',
        8 => '台港澳地区'
    ];

    public function _initialize()
    {
        parent::_initialize();
    }

    // 获取区域
    public function getAreas()
    {
        try {
            $res = Db::name('extend_areas')->where([
                'region_type' => ['<>', 0],
                'area_type' => 1,
            ])->select();
            $areaArr = [];
            if (!empty($res)) {
                foreach ($res as $k => $v) {
                    $areaArr[$this->areaName[$v['region_type']]][] = $v;
                }
            }
            unset($areaArr['台港澳地区']);
            returnApi(true, '列表返回成功', $areaArr);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 获取区域
    public function getAreasIndex()
    {
        try {
            $res = Db::name('extend_areas')->where([
                'region_type' => ['<>', 0],
                'area_type' => 1,
            ])->select();
            $areaArr = [];
            if (!empty($res)) {
                foreach ($res as $k => $v) {
                    $areaArr[$v['region_type']][] = $v;
                }
            }
            $newArr = [];
            foreach ($areaArr as $k => $v) {
                $newArr[] = $v;
            }
            returnApi(true, '列表返回成功', $newArr);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 获取城市
    public function getCity()
    {
        try {
            $param = input('post.');
            if (empty($param['cityId'])) {
                throw new Exception('cityId为空');
            }
            $res = Db::name('extend_areas')->where('parent_id', $param['cityId'])->select();
            returnApi(true, '成功', $res);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 获取banner
    public function getBanner()
    {
        try {
            $res = Db::name('app_banner')->where('status', 1)->where('type', 2)->select();
            if (!empty($res)) {
                foreach ($res as $k => &$v) {
                    $v['banner_path'] = config('USER_IMG_HOST') . '/' . $v['path'];
                }
            }
            returnApi(true, '成功', $res);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 升级
    public function version()
    {
        try {
            $input = input('post.');
            $where = array(
                'sys' => $input['app'],
                'type' => $input['type'],
                "versionCode" => ['>', $input['version']]
            );
            $ret = Db::name('app_version')->where($where)->order('versionCode desc')->select();
            $retAppInfo = [
                'url' => '',
                'version' => '',
                'size' => ''
            ];
            if ($ret) {
                $appinfo = $ret[0];
                $isnew = true;
                foreach ($ret as $k => $v) {
                    if ((int)$v['isForce'] == 1) {
                        $isforce = true;
                        break;
                    } else {
                        $isforce = false;
                    }
                }
                $retAppInfo = [
                    'url' => config('USER_IMG_HOST') . $appinfo['downUrl'],
                    'version' => $appinfo['versionName'],
                    'size' => $appinfo['size']
                ];
            } else {
                $isnew = false;
                $isforce = false;
            }
            $result = array(
                'isnew' => $isnew,
                'isforce' => $isforce,
            );
            $res = array_merge($result, $retAppInfo);
            returnApi(true, '成功', $res);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    public function inviteFriends()
    {
        try {
            returnApi(true, '成功', [
                'integration' => config('inviteFriends.integration'),
                'money' => config('inviteFriends.money')
            ]);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 2小时
    public function cancelUnpaidOrder()
    {
        try {
            $DriverItineraryModel = model('DriverItinerary');
            $DriverItineraryTruckModel = model('DriverItineraryTruck');
            // 查找司机超过2小时的行程
            $DriverItinerary = $DriverItineraryModel->where([
                'date' => ['<', time()]
            ])->where([
                'date' => ['<>', 0],
                'status' => 1
            ])->select();
            $DriverItineraryTruck = $DriverItineraryTruckModel->where([
                'date' => ['<', time()]
            ])->where([
                'date' => ['<>', 0],
                'status' => 1
            ])->select();
            $DriverItineraryId = '';
            $DriverItineraryTruckId = '';
            $driverId = '';
            $driverTId = '';
            // 现在过去两小时内的
            if (!empty($DriverItinerary)) {
                foreach ($DriverItinerary as $key => $val) {
                    $DriverItineraryId .= $val['itineraryId'] . ',';
                    $driverId .= $val['driverId'] . ',';
                }
            }
            if (!empty($DriverItineraryTruck)) {
                foreach ($DriverItineraryTruck as $key => $val) {
                    $DriverItineraryTruckId .= $val['itineraryId'] . ',';
                    $driverTId .= $val['driverId'] . ',';
                }
            }
            $driverId = rtrim($driverId, ',');
            $driverTId = rtrim($driverTId, ',');
            $DriverItineraryId = rtrim($DriverItineraryId, ',');
            $DriverItineraryTruckId = rtrim($DriverItineraryTruckId, ',');
            if (!empty($DriverItineraryId)) {
                $DriverItineraryModel->where('itineraryId', 'IN', $DriverItineraryId)->update([
                    'status' => 10
                ]);
            }
            if (!empty($DriverItineraryTruckId)) {
                $DriverItineraryTruckModel->where('itineraryId', 'IN', $DriverItineraryTruckId)->update([
                    'status' => 10
                ]);
            }
            $driverIdArr = [];
            if (!empty($driverId)) {
                $list = model('driver')->where('driverId', 'IN', $driverId)->select();
                foreach ($list as $k => $v) {
                    $driverIdArr[] = (string)$v['alias'];
                }
            }
            $driverTIdArr = [];
            if (!empty($driverTId)) {
                $list = model('Driver')->where('driverId', 'IN', $driverTId)->select();
                foreach ($list as $k => $v) {
                    $driverTIdArr[] = (string)$v['alias'];
                }
            }
            if ($driverIdArr) {
                sendPush([
                    'type' => 'd',
                    'Content' => '您有顺风车行程过期',
                    'Title' => '您有顺风车行程过期',
                    'alias' => $driverIdArr,
                    'data' => [
                        'code' => '2888',
                        'sound' => '您有顺风车行程过期',
                        'content' => '您有顺风车行程过期',
                        'data' => [],
                        'title' => '您有顺风车行程过期'
                    ],
                    'extras' => []
                ]);
            }
            if ($driverTIdArr) {
                sendPush([
                    'type' => 'd',
                    'Content' => '您有货急送行程过期',
                    'Title' => '您有货急送行程过期',
                    'alias' => $driverTIdArr,
                    'data' => [
                        'code' => '2889',
                        'sound' => '您有货急送行程过期',
                        'content' => '您有货急送行程过期',
                        'data' => [],
                        'title' => '您有货急送行程过期'
                    ],
                    'extras' => []
                ]);
            }
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    public function authPush()
    {
        try {
            $driverId = input('post.driverId');
            if (empty($driverId)) {
                throw new Exception('司机id必传');
            }
            $type = input('post.type');
            if (empty($type)) {
                throw new Exception('类型必传');
            }
            $driverInfo = Db::name('driver')->where('driverId', $driverId)->find();
            if (empty($driverInfo)) {
                throw new Exception('司机信息为空');
            }
            $reason = Db::name('driver_auth_refuse_reason')->where('did', $driverId)->find();
            $res = sendPush([
                'type' => 'd',
                'Content' => '您有新的认证信息',
                'Title' => '您有新的认证信息',
                'alias' => $driverInfo['alias'],
                'data' => [
                    'code' => '2417',
                    'sound' => '您有新的认证信息',
                    'content' => '您有新的认证信息',
                    'data' => [
                        'type' => $type, //  1是司机,2是快递
                        'reason' => isset($reason['reason']) && !empty($reason['reason']) ? $reason['reason'] : '无备注',
                        'authStatus' => $driverInfo['authStatus'],
                        'expressAuthStatus' => $driverInfo['expressAuthStatus']
                    ],
                    'title' => '您有新的认证信息'
                ],
                'extras' => []
            ]);
            px($res);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    public function getNearbyList()
    {
        try {
            $param = input('post.');
            $arr = [
                6 => '超市',
                7 => '咖啡',
                8 => '药店',
                9 => '烟酒',
                12 => '餐厅',
                13 => '水果',
                11 => '超市'
            ];
            $list = BaiduMap::getNearbyList($param['lng'], $param['lat'], $arr[$param['id']]);
            returnApi(true, '成功', $list);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }


}